﻿#include <QColor>
#include<QMouseEvent>
#include<QScrollBar>
#include "EditTreeWidget.h"
#include "XTWidget.h"


EditTreeWidget::EditTreeWidget(QWidget* parent)
	:QTreeWidget(parent), m_change(true)
{
	sbvalue=-1;	
	this->setHeaderHidden(true);
	this->setIndentation(0);
	this->setColumnCount(1);

	this->setExpandsOnDoubleClick(false);
	this->setSelectionMode(QAbstractItemView::NoSelection);
	this->setFocusPolicy(Qt::FocusPolicy::NoFocus);
	this->setFixedWidth(250);
	this->setVerticalScrollMode(ScrollMode::ScrollPerPixel);
	this->setCursor(QCursor(Qt::ArrowCursor));

	//this->setAcceptDrops(true);
	/*height[0]=122,height[1]=170,height[2]=206,height[3]=138,height[4]=106, height[5] = 138, height[6]=150;

	QScrollBar *sb=this->verticalScrollBar();
	connect(sb,&QScrollBar::valueChanged,this,&EditTreeWidget::OnScrollBarValueChanged);
	setIconSize(QSize(32, 32));

	QIcon icon;
	icon.addFile(QStringLiteral(":/new/Resources/new/main/model_normal.png.png"), QSize(), QIcon::Normal, QIcon::Off);
	icon.addFile(QStringLiteral(":/new/Resources/new/main/model_select.png.png"), QSize(), QIcon::Normal, QIcon::On);
	QBrush brush(QColor(0, 170, 255, 255));
	brush.setStyle(Qt::NoBrush);
	QFont font;
	font.setFamily(QStringLiteral("Arial"));
	font.setPointSize(12);
	QIcon icon1;
	icon1.addFile(QStringLiteral(":/new/Resources/new/main/move_normal.png.png"), QSize(), QIcon::Normal, QIcon::Off);
	icon1.addFile(QStringLiteral(":/new/Resources/new/main/move_select.png.png"), QSize(), QIcon::Normal, QIcon::On);
	QIcon icon2;
	icon2.addFile(QStringLiteral(":/new/Resources/new/main/retate_normal.png.png"), QSize(), QIcon::Normal, QIcon::Off);
	icon2.addFile(QStringLiteral(":/new/Resources/new/main/retate_select.png.png"), QSize(), QIcon::Normal, QIcon::On);
	QIcon icon3;
	icon3.addFile(QStringLiteral(":/new/Resources/new/main/scale_normal.png.png"), QSize(), QIcon::Normal, QIcon::Off);
	icon3.addFile(QStringLiteral(":/new/Resources/new/main/scale_select.png.png"), QSize(), QIcon::Normal, QIcon::On);
	QIcon icon4;
	icon4.addFile(QStringLiteral(":/new/Resources/new/main/cut_normal.png"), QSize(), QIcon::Normal, QIcon::Off);
	icon4.addFile(QStringLiteral(":/new/Resources/new/main/cut_select.png"), QSize(), QIcon::Normal, QIcon::On);
	QIcon icon5;
	icon5.addFile(QStringLiteral(":/new/Resources/new/main/support_normal.png"), QSize(), QIcon::Normal, QIcon::Off);
	icon5.addFile(QStringLiteral(":/new/Resources/new/main/support_select.png"), QSize(), QIcon::Normal, QIcon::On);

	QTreeWidgetItem *__qtreewidgetitem1 = new QTreeWidgetItem(this);
	__qtreewidgetitem1->setFont(0, font);
	__qtreewidgetitem1->setForeground(0, brush);
	__qtreewidgetitem1->setIcon(0, icon);
	__qtreewidgetitem1->setText(0, "   Model     \342\227\242");
	QTreeWidgetItem *__qtreewidgetitem2 = new QTreeWidgetItem(this);
	__qtreewidgetitem2->setFont(0, font);
	__qtreewidgetitem2->setIcon(0, icon1);
	__qtreewidgetitem2->setText(0, "   Model     \342\227\242");
	QTreeWidgetItem *__qtreewidgetitem3 = new QTreeWidgetItem(this);
	__qtreewidgetitem3->setFont(0, font);
	__qtreewidgetitem3->setIcon(0, icon2);
	QTreeWidgetItem *__qtreewidgetitem4 = new QTreeWidgetItem(this);
	__qtreewidgetitem4->setFont(0, font);
	__qtreewidgetitem4->setIcon(0, icon3);
	QTreeWidgetItem *__qtreewidgetitem5 = new QTreeWidgetItem(this);
	__qtreewidgetitem5->setFont(0, font);
	__qtreewidgetitem5->setIcon(0, icon4);
	QTreeWidgetItem *__qtreewidgetitem6 = new QTreeWidgetItem(this);
	__qtreewidgetitem6->setFont(0, font);
	__qtreewidgetitem6->setIcon(0, icon5);*/
}
EditTreeWidget::~EditTreeWidget()
{

}

void EditTreeWidget::Bind(QString filepath)
{
	QIcon icon1;
	icon1.addFile(QStringLiteral(":/new/Resources/new/main/move_normal.png.png"), QSize(), QIcon::Normal, QIcon::Off);
	icon1.addFile(QStringLiteral(":/new/Resources/new/main/move_select.png.png"), QSize(), QIcon::Normal, QIcon::On);

	QBrush brush(QColor(0, 170, 255, 255));
	brush.setStyle(Qt::NoBrush);
	QFont font;
	font.setFamily(QStringLiteral("Arial"));
	font.setPointSize(12);

	if (!QFile::exists(filepath))
	{
		return;
	}
	QFile file(filepath);
	QStringList lines;
	if (file.open(QFile::ReadOnly)) {
		
		char buf[4096];
		while (!file.atEnd())
		{
			qint64 lineLength = file.readLine(buf, sizeof(buf));
			if (lineLength != -1) {
				// the line is available in buf
				lines.append(QString(buf));
			}
		}
	
	}
	file.close();

	for (int i = 0; i < lines.size(); ++i)
	{
		QStringList vname  = lines.at(i).split(";");
		if (vname.size() < 1)
			return;
		QString subtitle = vname.at(0);
		

		QTreeWidgetItem *__qtreewidgetitem1 = new QTreeWidgetItem(this);
		__qtreewidgetitem1->setFont(0, font);
		__qtreewidgetitem1->setForeground(0, brush);
		__qtreewidgetitem1->setIcon(0, icon1);
		subtitle = QString("   %1     \342\227\242").arg(subtitle);
		__qtreewidgetitem1->setText(0, subtitle);

		XTWidget* subw = new XTWidget(this);
		subw->InitUI(lines.at(i));
		vWidget.push_back(subw);
		height[i] = subw->m_hight;

	}
	
	/*  mlw= new QWidget(this);
	  mmw = new QWidget(this);
	  rmw = new QWidget(this);
	  smw = new QWidget(this);
	  cmw = new QWidget(this);
	  msw = new QWidget(this);



	  QWidget* w[6]={mlw,mmw,rmw,smw,cmw,msw};*/
	for (int i = 0; i<lines.size(); i++)
    {
		QTreeWidgetItem* child=new QTreeWidgetItem(this->topLevelItem(i));
		child->setSizeHint(0, QSize(EWIDE, height[i]));
        this->topLevelItem(i)->addChild(child);
		this->topLevelItem(i)->setSizeHint(0, QSize(EWIDE, TITLE));
		this->topLevelItem(i)->setBackground(0,QBrush(QColor(30,30,30)));
		this->setItemWidget(child, 0, vWidget[i]);
    }
}

int EditTreeWidget::GetWidgetHeight()
{
    int n=this->topLevelItemCount();
    int h=0;
    for(int i=0;i<n;i++)
    {
        QTreeWidgetItem* head=this->topLevelItem(i);
        h+=head->sizeHint(0).height();
        if(head->isExpanded())
            h+=height[i];
    }
    return h;
}

int EditTreeWidget::GetNumofExpand()
{
    int n=this->topLevelItemCount(),nexpand=0;
    for(int i=0;i<n;i++)
		if (this->topLevelItem(i)->isExpanded())
		{
			nexpand++;			
			this->topLevelItem(i)->setBackground(0, QBrush(QColor(28, 44, 54, 255)));
			this->topLevelItem(i)->setForeground(0, QBrush(QColor(84, 200, 234, 255)));

			if (i == 5)
			{
				if (!m_change)
				{
					emit changeStyle(true);
					m_change = true;
				}				
			}
		
		}
		else
		{
			this->topLevelItem(i)->setBackground(0, QBrush(QColor(28, 44, 54, 255)));
			this->topLevelItem(i)->setForeground(0, QBrush(QColor(255, 255, 255, 255)));

			if (i == 5)
			{
				if (m_change)
				{
					emit changeStyle(false);
					m_change = false;
				}
				
			}
			
		}
           
    return nexpand;
}

void EditTreeWidget::mouseReleaseEvent(QMouseEvent *event)
{	
	if (event->pos().x() < 5)
		emit sendMouseRelease(event);
    if(event->button()&Qt::RightButton)
    {
		
    }
    else if(event->button()&Qt::LeftButton)
    {
        QTreeWidgetItem* cur=itemFromIndex(indexAt(event->pos()));
        if(cur&&cur->childCount()>0)
        {
            if(cur->isExpanded())
                cur->setExpanded(false);
            else
                cur->setExpanded(true);
			ResizeControl();
        }
    }
}
void EditTreeWidget::OnScrollBarValueChanged(int value)
{
    if(dataChanged==false)
        return;
    dataChanged=false;
    QScrollBar *sb=this->verticalScrollBar();
    if(sbvalue==-1)
    {
        sbvalue=0;
    }
    else
    {
        if(value-sbvalue>200||value==sb->maximum())
            value=std::min(sbvalue+20,sb->maximum());
        if(value-sbvalue<-200||value==sb->minimum())
            value=std::max(sbvalue-20,sb->minimum());
        sbvalue=value;
    }
    sb->setValue(sbvalue);
    dataChanged=true;
}

void EditTreeWidget::update()
{
	/*mlw->update();
	mmw->update();
	rmw->update();
	smw->update();
	msw->update();*/
}

int EditTreeWidget::heightValue()
{
	return this->rect().height();
}

void EditTreeWidget::ResizeControl()
{
	int h = this->GetWidgetHeight() + 10;
	h = std::min(h, 680);
	this->resize(QSize(400, h));
}

void EditTreeWidget::on_checked_change(int state)
{
	QMessageBox msgBox;
	QString infor = QString("the tool is %1, state is %2").arg(this->sender()->objectName()).arg(state);
	msgBox.setText("infor ....");
	msgBox.setInformativeText(infor);
	msgBox.setStandardButtons(QMessageBox::Ok);
	int ret = msgBox.exec();
}

void EditTreeWidget::on_combobox_change(const QString& text)
{
	QMessageBox msgBox;
	QString infor = QString("the tool is %1, state is %2").arg(this->sender()->objectName()).arg(text);
	msgBox.setText("infor ....");
	msgBox.setInformativeText(infor);
	msgBox.setStandardButtons(QMessageBox::Ok);
	int ret = msgBox.exec();

}

void EditTreeWidget::on_slider_change(int state)
{
	QMessageBox msgBox;
	QString infor = QString("the tool is %1, state is %2").arg(this->sender()->objectName()).arg(state);
	msgBox.setText("infor ....");
	msgBox.setInformativeText(infor);
	msgBox.setStandardButtons(QMessageBox::Ok);
	int ret = msgBox.exec();
}

void EditTreeWidget::on_spinbox_change(int value)
{
	QMessageBox msgBox;
	QString infor = QString("the tool is %1, state is %2").arg(this->sender()->objectName()).arg(value);
	msgBox.setText("infor ....");
	msgBox.setInformativeText(infor);
	msgBox.setStandardButtons(QMessageBox::Ok);
	int ret = msgBox.exec();

}





